package com.epfresh.supplier.dialog;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.epfresh.api.widget.Dialog.BaseDialogView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.OfferReasonItemAdapter;
import com.epfresh.supplier.entity.OfferListEntity;
import com.epfresh.supplier.entity.OfferListEntity.SupplyGiveupReasonBean;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

/**
 * @author zhaozhenqiang
 * @version 1.0
 * @date create 18-4-18
 * @Description
 */
public class OfferReasonListDialog extends BaseDialogView {

    public OfferReasonListDialog(Context context) {
        super(context);
/*        setInAnim(true);
        setOutAnim(true);
        inAnim.setDuration(300);
        outAnim.setDuration(300);*/

    }

    public Animation getInAnimation() {

        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_bottom_in);
    }

    public Animation getOutAnimation() {
        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_bottom_out);
    }

    @Override
    public void initBefore() {
        gravity = Gravity.CENTER;
    }

    @Override
    protected void initRes() {
        outContainerRes = R.id.f_out_container;
        containerRes = R.id.f_content_container;
        rootRes = R.layout.dialog_content_view;
        rootBgRes = R.id.f_v_content_bg;
    }

    protected ViewGroup getDecorView(Context context) {
        return (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
    }

    @Override
    protected void createdView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View rootView = layoutInflater.inflate(R.layout.dialog_offer_reason_list, null, false);
        ViewGroup.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rootView.setLayoutParams(lp);
        setContentView(rootView);
//        rootView.setClickable(true);
        setCancelable(true);
        initView();
    }

    RecyclerView rv;
    MultiTypeAdapter multiAdapter;
    OfferReasonItemAdapter adapter;
    List<OfferListEntity.SupplyGiveupReasonBean> items = new ArrayList<>();

    OnSelectedListener onSelectedListener;
    private void initView() {

        rv = findView(R.id.rv);

        findView(R.id.tv_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        GridLayoutManager mManager;
        mManager = new GridLayoutManager(context, 2);
        rv.setLayoutManager(mManager);
        if (adapter == null)
            adapter = new OfferReasonItemAdapter();
        if (multiAdapter == null)
            multiAdapter = new MultiTypeAdapter();
        multiAdapter.register(OfferListEntity.SupplyGiveupReasonBean.class, adapter);
        rv.setAdapter(multiAdapter);
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i, Object o) {

                dismiss();
                if (onSelectedListener != null){
                    onSelectedListener.onSelected((OfferListEntity.SupplyGiveupReasonBean) o);
                }

            }
        });


        multiAdapter.setItems(items);
    }

    public void show(List<OfferListEntity.SupplyGiveupReasonBean> list) {
        items.clear();
        if (list != null && list.size() > 0) {

            items.addAll(list);
            show();
            multiAdapter.notifyDataSetChanged();
        }
    }

    public OnSelectedListener getOnSelectedListener() {
        return onSelectedListener;
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public interface OnSelectedListener{
        void onSelected(OfferListEntity.SupplyGiveupReasonBean item);
    }
}
